﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KMFrame.Permission.BLL;
using KMFrame.Util;

namespace WebSite.admin.users
{
    public partial class useredit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int userid = int.Parse(Request["id"]);
                Users ubll = new Users();
                UserInfo vUserEdit = ubll.GetUserInfo(userid);

                this.UserEdit_userid.Value = vUserEdit.UserID.ToString();
                this.UserEdit_username.Value = vUserEdit.UserName;
                this.UserEdit_usergroup.SelectedValue = vUserEdit.UserGroup.ToString();
                this.UserEdit_email.Value = vUserEdit.Email;
                this.UserEdit_status.SelectedValue = vUserEdit.Status.ToString();
                this.UserEdit_createtime.Value = vUserEdit.CreateTime.ToShortDateString();
                this.UserEdit_lastlogintime.Value = vUserEdit.LastLoginTime.ToString();

                Groups gbll = new Groups();
                IEnumerable<Groups.GroupInfo> glist = gbll.GetGroupInfoList();
                foreach (Groups.GroupInfo g in glist)
                {
                    ListItem item = new ListItem(g.GroupName, g.GroupID.ToString());
                    this.UserEdit_usergroup.Items.Add(item);
                }
                this.UserEdit_usergroup.SelectedValue = vUserEdit.UserGroup.ToString();
            }
            else
            {
                UserInfo user = new UserInfo();
                user.UserID = int.Parse(Request.Form["UserEdit_userid"]);
                user.UserName = Request.Form["UserEdit_username"];
                user.Email = Request.Form["UserEdit_email"];
                user.UserGroup = int.Parse(Request.Form["UserEdit_usergroup"]);
                user.Status = int.Parse(Request.Form["UserEdit_status"]);

                Users ubll = new Users();
                ubll.UpdateUserInfo(user);
                Response.End();

                //AjaxDone ajax = new AjaxDone();
                //ajax.statusCode = "200";
                //ajax.navTabId = "";
                //ajax.callbackType = "closeCurrent";
                //Response.Write(JsonHelper.ObjectToJSON(ajax));
                //Response.End();
                //Response.Write(@"{"statusCode":"200","message":"\u64cd\u4f5c\u6210\u529f","navTabId":"","callbackType":"closeCurrent","forwardUrl":""}";
            }
        }
    }

    public class AjaxDone
    {
        public string statusCode { get; set; }
        public string message { get; set; }
        public string navTabId { get; set; }
        public string callbackType { get; set; }
        public string forwardUrl { get; set; }
    }
}